home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / text / cmanual.lzh / ACM2.lzh / Menus / Example1.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  14KB  |  371 lines

  1. /* Example1                                                             */
  2. /* This program opens a normal window to which we connect a menu strip. */
  3. /* The menu will look like this:                                        */
  4. /*                                                                      */
  5. /* Mode                                                                 */
  6. /* -----------------                                                    */
  7. /* | v Plain       |                                                    */
  8. /* |   Bold        |                                                    */
  9. /* |   Underlined  |                                                    */
  10. /* |   Italic      |                                                    */
  11. /* -----------------                                                    */
  12. /*                                                                      */
  13. /* The user can select either Plain or a combination of the other       */
  14. /* styles. (If the user selects Plain all other modes will be mutual    */
  15. /* excluded, but if the user on the other hand selects Bold, Underlined */
  16. /* or Italic, the Plain option will be mutul excluded.                  */
  17. /*                                                                      */
  18. /* This example also shows how a program should handle the IDCMP flags, */
  19. /* and how to collect several messages from one single menu event.      */
  20.  
  21.  
  22.  
  23. #include <intuition/intuition.h>
  24.  
  25.  
  26.  
  27. struct IntuitionBase *IntuitionBase;
  28.  
  29.  
  30.  
  31. /*************************************************************************/
  32. /*                         F O U R T H   I T E M                         */
  33. /*************************************************************************/
  34.  
  35. /* The text for the fourth item: */
  36. struct IntuiText my_fourth_text=
  37. {
  38.   2,            /* FrontPen, black. */
  39.   0,            /* BackPen, not used since JAM1. */
  40.   JAM1,         /* DrawMode, do not change the background. */
  41.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  42.                 /* This will leave enough space for the check mark. */
  43.   1,            /* TopEdge, 1 line down. */
  44.   NULL,         /* TextAttr, default font. */
  45.   "Italic",     /* IText, the string. */
  46.   NULL          /* NextItem, no link to other IntuiText structures. */
  47. };
  48.  
  49. /* The MenuItem structure for the fourth item: */
  50. struct MenuItem my_fourth_item=
  51. {
  52.   NULL,            /* NextItem, this is the last item in the list. */
  53.   0,               /* LeftEdge, 0 pixels out. */
  54.   30,              /* TopEdge, 30 lines down. */
  55.   150,             /* Width, 150 pixels wide. */
  56.   10,              /* Height, 10 lines high. */
  57.   ITEMTEXT|        /* Flags, render this item with text. */
  58.   ITEMENABLED|     /*        this item will be enabled. */
  59.   CHECKIT|         /*        it is an attribute item. */
  60.   HIGHCOMP,        /*        complement the colours when highlihted. */
  61.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  62.   (APTR) &my_fourth_text, /* ItemFill, pointer to the text. */
  63.   NULL,            /* SelectFill, nothing since we complement the col. */
  64.   0,               /* Command, no command-key sequence. */
  65.   NULL,            /* SubItem, no subitem list. */
  66.   MENUNULL,        /* NextSelect, no items selected. */
  67. };
  68.  
  69.  
  70.  
  71. /*************************************************************************/
  72. /*                          T H I R D   I T E M                          */
  73. /*************************************************************************/
  74.  
  75. /* The text for the third item: */
  76. struct IntuiText my_third_text=
  77. {
  78.   2,            /* FrontPen, black. */
  79.   0,            /* BackPen, not used since JAM1. */
  80.   JAM1,         /* DrawMode, do not change the background. */
  81.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  82.                 /* This will leave enough space for the check mark. */
  83.   1,            /* TopEdge, 1 line down. */
  84.   NULL,         /* TextAttr, default font. */
  85.   "Underlined", /* IText, the string. */
  86.   NULL          /* NextItem, no link to other IntuiText structures. */
  87. };
  88.  
  89. /* The MenuItem structure for the third item: */
  90. struct MenuItem my_third_item=
  91. {
  92.   &my_fourth_item, /* NextItem, linked to the fourth item. */
  93.   0,               /* LeftEdge, 0 pixels out. */
  94.   20,              /* TopEdge, 20 lines down. */
  95.   150,             /* Width, 150 pixels wide. */
  96.   10,              /* Height, 10 lines high. */
  97.   ITEMTEXT|        /* Flags, render this item with text. */
  98.   ITEMENABLED|     /*        this item will be enabled. */
  99.   CHECKIT|         /*        it is an attribute item. */
  100.   HIGHCOMP,        /*        complement the colours when highlihted. */
  101.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  102.   (APTR) &my_third_text, /* ItemFill, pointer to the text. */
  103.   NULL,            /* SelectFill, nothing since we complement the col. */
  104.   0,               /* Command, no command-key sequence. */
  105.   NULL,            /* SubItem, no subitem list. */
  106.   MENUNULL,        /* NextSelect, no items selected. */
  107. };
  108.  
  109.  
  110.  
  111. /*************************************************************************/
  112. /*                         S E C O N D   I T E M                         */
  113. /*************************************************************************/
  114.  
  115. /* The text for the second item: */
  116. struct IntuiText my_second_text=
  117. {
  118.   2,          /* FrontPen, black. */
  119.   0,          /* BackPen, not used since JAM1. */
  120.   JAM1,       /* DrawMode, do not change the background. */
  121.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  122.               /* This will leave enough space for the check mark. */
  123.   1,          /* TopEdge, 1 line down. */
  124.   NULL,       /* TextAttr, default font. */
  125.   "Bold",     /* IText, the string. */
  126.   NULL        /* NextItem, no link to other IntuiText structures. */
  127. };
  128.  
  129. /* The MenuItem structure for the second item: */
  130. struct MenuItem my_second_item=
  131. {
  132.   &my_third_item,  /* NextItem, linked to the third item. */
  133.   0,               /* LeftEdge, 0 pixels out. */
  134.   10,              /* TopEdge, 10 lines down. */
  135.   150,             /* Width, 150 pixels wide. */
  136.   10,              /* Height, 10 lines high. */
  137.   ITEMTEXT|        /* Flags, render this item with text. */
  138.   ITEMENABLED|     /*        this item will be enabled. */
  139.   CHECKIT|         /*        it is an attribute item. */
  140.   HIGHCOMP,        /*        complement the colours when highlihted. */
  141.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  142.   (APTR) &my_second_text, /* ItemFill, pointer to the text. */
  143.   NULL,            /* SelectFill, nothing since we complement the col. */
  144.   0,               /* Command, no command-key sequence. */
  145.   NULL,            /* SubItem, no subitem list. */
  146.   MENUNULL,        /* NextSelect, no items selected. */
  147. };
  148.  
  149.  
  150.  
  151. /*************************************************************************/
  152. /*                          F I R S T   I T E M                          */
  153. /*************************************************************************/
  154.  
  155. /* The text for the first item: */
  156. struct IntuiText my_first_text=
  157. {
  158.   2,          /* FrontPen, black. */
  159.   0,          /* BackPen, not used since JAM1. */
  160.   JAM1,       /* DrawMode, do not change the background. */
  161.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  162.               /* This will leave enough space for the check mark. */
  163.   1,          /* TopEdge, 1 line down. */
  164.   NULL,       /* TextAttr, default font. */
  165.   "Plain",    /* IText, the string. */
  166.   NULL        /* NextItem, no link to other IntuiText structures. */
  167. };
  168.  
  169. /* The MenuItem structure for the first item: */
  170. struct MenuItem my_first_item=
  171. {
  172.   &my_second_item, /* NextItem, linked to the second item. */
  173.   0,               /* LeftEdge, 0 pixels out. */
  174.   0,               /* TopEdge, 0 lines down. */
  175.   150,             /* Width, 150 pixels wide. */
  176.   10,              /* Height, 10 lines high. */
  177.   ITEMTEXT|        /* Flags, render this item with text. */
  178.   ITEMENABLED|     /*        this item will be enabled. */
  179.   CHECKIT|         /*        it is an attribute item. */
  180.   CHECKED|         /*        this item is initially selected. */
  181.   HIGHCOMP,        /*        complement the colours when highlihted. */
  182.   0xFFFFFFFE,      /* MutualExclude, mutualexclude all items except the */
  183.                    /*                first one. */
  184.   (APTR) &my_first_text, /* ItemFill, pointer to the text. */
  185.   NULL,            /* SelectFill, nothing since we complement the col. */
  186.   0,               /* Command, no command-key sequence. */
  187.   NULL,            /* SubItem, no subitem list. */
  188.   MENUNULL,        /* NextSelect, no items selected. */
  189. };
  190.  
  191.  
  192.  
  193. /*************************************************************************/
  194. /*                              M E N U                                  */
  195. /*************************************************************************/
  196.  
  197. /* The Menu structure for the first (and only) menu: */
  198. struct Menu my_menu=
  199. {
  200.   NULL,          /* NextMenu, no more menu structures. */
  201.   0,             /* LeftEdge, left corner. */
  202.   0,             /* TopEdge, for the moment ignored by Intuition. */
  203.   50,            /* Width, 50 pixels wide. */
  204.   0,             /* Height, for the moment ignored by Intuition. */
  205.   MENUENABLED,   /* Flags, this menu will be enabled. */
  206.   "Mode",        /* MenuName, the string. */
  207.   &my_first_item /* FirstItem, pointer to the first item in the list. */
  208. };
  209.  
  210.  
  211.  
  212. /* Declare a pointer to a Window structure: */ 
  213. struct Window *my_window;
  214.  
  215. /* Declare and initialize your NewWindow structure: */
  216. struct NewWindow my_new_window=
  217. {
  218.   50,            /* LeftEdge    x position of the window. */
  219.   25,            /* TopEdge     y positio of the window. */
  220.   200,           /* Width       200 pixels wide. */
  221.   100,           /* Height      100 lines high. */
  222.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  223.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  224.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  225.                  /*             user has selected the Close window gad. */
  226.   MENUPICK,
  227.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  228.   WINDOWCLOSE|   /*             Close Gadget. */
  229.   WINDOWDRAG|    /*             Drag gadget. */
  230.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  231.   WINDOWSIZING|  /*             Sizing Gadget. */
  232.   ACTIVATE,      /*             The window should be Active when opened. */
  233.   NULL,          /* FirstGadget No Custom gadgets. */
  234.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  235.   "Style Editor",/* Title       Title of the window. */
  236.   NULL,          /* Screen      Connected to the Workbench Screen. */
  237.   NULL,          /* BitMap      No Custom BitMap. */
  238.   80,            /* MinWidth    We will not allow the window to become */
  239.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  240.   300,           /* MaxWidth    than 300 x 200. */
  241.   200,           /* MaxHeight */
  242.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  243. };
  244.  
  245.  
  246.  
  247. main()
  248. {
  249.   /* Boolean variable used for the while loop: */
  250.   BOOL close_me;
  251.  
  252.   /* Declare a variable in which we will store the IDCMP flag: */
  253.   ULONG class;
  254.   
  255.   /* If we recieve a MENUPICK event, the Code field of the message */
  256.   /* structure will contain the menu number of the first selected item. */
  257.   /* Declare a variable to store the Code value in, and an extra menu */
  258.   /* number variable: */
  259.   USHORT code, menu_number;
  260.   
  261.   /* Declare a MenuItem pointer: */
  262.   struct MenuItem *item;
  263.   
  264.   /* Declare a pointer to an IntuiMessage structure: */
  265.   struct IntuiMessage *my_message;
  266.  
  267.  
  268.  
  269.   /* Before we can use Intuition we need to open the Intuition Library: */
  270.   IntuitionBase = (struct IntuitionBase *)
  271.     OpenLibrary( "intuition.library", 0 );
  272.   
  273.   if( IntuitionBase == NULL )
  274.     exit(); /* Could NOT open the Intuition Library! */
  275.  
  276.  
  277.  
  278.   /* We will now try to open the window: */
  279.   my_window = (struct Window *) OpenWindow( &my_new_window );
  280.   
  281.   /* Have we opened the window succesfully? */
  282.   if(my_window == NULL)
  283.   {
  284.     /* Could NOT open the Window! */
  285.     
  286.     /* Close the Intuition Library since we have opened it: */
  287.     CloseLibrary( IntuitionBase );
  288.  
  289.     exit();  
  290.   }
  291.  
  292.  
  293.  
  294.   /* We have opened the window, and everything seems to be OK. */
  295.  
  296.  
  297.  
  298.   SetMenuStrip( my_window, &my_menu );
  299.   printf("Menustrip connected to window!\n");
  300.  
  301.  
  302.   close_me = FALSE;
  303.  
  304.   /* Stay in the while loop until the user has selected the Close window */
  305.   /* gadget: */
  306.   while( close_me == FALSE )
  307.   {
  308.     /* Wait until we have recieved a message: */
  309.     Wait( 1 << my_window->UserPort->mp_SigBit );
  310.  
  311.     /* As long as we collect messages sucessfully we stay in the loop: */
  312.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  313.     {
  314.       /* After we have collected the message we can read it, and save any */
  315.       /* important values which we maybe want to check later: */
  316.       class = my_message->Class;
  317.       code = my_message->Code;
  318.  
  319.  
  320.       /* After we have read it we reply as fast as possible: */
  321.       /* REMEMBER! Do never try to read a message after you have replied! */
  322.       /* Some other process has maybe changed it. */
  323.       ReplyMsg( my_message );
  324.  
  325.       /* Check which IDCMP flag was sent: */
  326.       if( class == CLOSEWINDOW )
  327.         close_me=TRUE; /* The user selected the Close window gadget! */  
  328.  
  329.       if(class == MENUPICK)
  330.       {
  331.         printf("\nMenu pick!\n");
  332.         menu_number = code;
  333.         
  334.         while( menu_number != MENUNULL )
  335.         {
  336.           /* Get the address of the item: */
  337.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  338.  
  339.  
  340.           /* Print out the menu number plus etc: */
  341.           printf("menu_number= %d\n", menu_number );
  342.           printf("MENUNUM = %d\n", MENUNUM(menu_number) );
  343.           printf("ITEMNUM = %d\n", ITEMNUM(menu_number) );
  344.           printf("SUBNUM  = %d\n", SUBNUM(menu_number) );
  345.  
  346.  
  347.           /* Get the following item's menu number: */
  348.           menu_number = item->NextSelect;
  349.         }
  350.       }
  351.     }
  352.   }
  353.  
  354.  
  355.  
  356.   printf("Menustrip removed from window!\n");
  357.   ClearMenuStrip( my_window );
  358.  
  359.  
  360.  
  361.   /* Close the window: */
  362.   CloseWindow( my_window );
  363.  
  364.  
  365.  
  366.   /* Close the Intuition Library since we have opened it: */
  367.   CloseLibrary( IntuitionBase );
  368.   
  369.   /* THE END */
  370. }
  371.